package videoCourse.Structure;

public class MyStack {
    int[] elements = new int[0];

    public void put(int num){

        int[] newArray = new int[elements.length+1];

        for(int i = 0;i<elements.length;i++){
            newArray[i] = elements[i];
        }

        newArray[elements.length] = num;

        elements = newArray;
    }

    public int pop(){
        if(elements.length == 0){
            throw new RuntimeException("stack is empty");
        }
        int target = elements[elements.length-1];
        int[] newArray = new int[elements.length-1];
        for(int i = 0;i<elements.length-1;i++){
            newArray[i] = elements[i];
        }
        elements = newArray;
        return target;
    }

    public int peek(){
        if(elements.length == 0){
            throw new RuntimeException("stack is empty");
        }
        return elements[elements.length-1];
    }

    public boolean isEmpty(){
        return elements.length==0;
    }
}
